package utility;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.Color;

public class UpdatePasswordDS extends JFrame {

    private JPanel contentPane;
    public static JTextField Username_textField;
    private JTextField oldPassword_Text;
    private JTextField newPassword_Text;
    private JTextField newPassword_Text_2;
    public static JComboBox comboBox;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UpdatePasswordDS frame = new UpdatePasswordDS();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UpdatePasswordDS() {
        setTitle("更改密码");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 376, 450);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("返回");
        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
            }
        });
        btnNewButton.setForeground(new Color(0, 0, 0));
        btnNewButton.setBackground(new Color(255, 255, 255));
        btnNewButton.setBounds(269, 327, 93, 23);
        contentPane.add(btnNewButton);

        JLabel username = new JLabel("用户名");
        username.setBounds(60, 91, 58, 15);
        username.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(username);

        JLabel oldPassword = new JLabel("原密码");
        oldPassword.setHorizontalAlignment(SwingConstants.CENTER);
        oldPassword.setBounds(60, 138, 58, 15);
        contentPane.add(oldPassword);

        JLabel newPassword = new JLabel("新密码");
        newPassword.setHorizontalAlignment(SwingConstants.CENTER);
        newPassword.setBounds(60, 193, 58, 15);
        contentPane.add(newPassword);

        JLabel newPassword_2 = new JLabel("密码验证");
        newPassword_2.setHorizontalAlignment(SwingConstants.CENTER);
        newPassword_2.setBounds(60, 237, 58, 15);
        contentPane.add(newPassword_2);

        JLabel Type = new JLabel("类型");
        Type.setHorizontalAlignment(SwingConstants.CENTER);
        Type.setBounds(60, 39, 58, 15);
        contentPane.add(Type);

        comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"学生修改密码", "老师修改密码", "管理员修改密码"}));
        comboBox.setBounds(117, 35, 210, 23);
        contentPane.add(comboBox);

        JButton UpdateButton = new JButton("修改");

        UpdateButton.setBounds(117, 327, 97, 23);
        contentPane.add(UpdateButton);

        Username_textField = new JTextField();

        Username_textField.setBounds(117, 88, 210, 21);
        contentPane.add(Username_textField);
        Username_textField.setColumns(10);

        oldPassword_Text = new JTextField();


        oldPassword_Text.setColumns(10);
        oldPassword_Text.setBounds(117, 135, 210, 21);
        contentPane.add(oldPassword_Text);

        newPassword_Text = new JTextField();
        newPassword_Text.setColumns(10);
        newPassword_Text.setBounds(117, 190, 210, 21);
        contentPane.add(newPassword_Text);

        newPassword_Text_2 = new JTextField();
        newPassword_Text_2.setColumns(10);
        newPassword_Text_2.setBounds(117, 234, 210, 21);
        contentPane.add(newPassword_Text_2);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setIcon(new ImageIcon("image//background.jpg"));
        lblNewLabel.setBounds(0, -24, 372, 447);
        contentPane.add(lblNewLabel);
        oldPassword_Text.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                int type=comboBox.getSelectedIndex();//0学生  1老师 2管理员
                if(type==0) {  //学生修改密码
                    String Upusername=Username_textField.getText();
                    String sqlName="select Sid from student where Sid='"+ Upusername +"';";   //查看用户名合法性
                    int valid= DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "用户名错误或者不存在");
                    }else{
                        JOptionPane.showMessageDialog(null, "查询到该学生");
                    }//
                }
                if(type==1) {  //老师修改密码
                    String Upusername=Username_textField.getText();
                    String sqlName="select Tid from Teacher where Tid='"+ Upusername +"';";   //查看用户名合法性
                    int valid=DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "用户名错误或者不存在");
                    }else {
                        JOptionPane.showMessageDialog(null, "查询到该老师");
                    }//
                }
                if(type==2) {  //管理员修改密码
                    String Upusername=Username_textField.getText();
                    String sqlName="select Aid from admin where Aid='"+ Upusername +"';";   //查看用户名合法性
                    int valid=DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "用户名错误或者不存在");
                    }else {
                        JOptionPane.showMessageDialog(null, "查询到该管理员");
                    }//
                }


            }
        });

//		----------------------------------检验用户名的合法性
//		----------------------------修改密码
        UpdateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int type=comboBox.getSelectedIndex();//0学生  1老师 2管理员
                if(type==0) {//学生修改密码
                    String Upusername=Username_textField.getText();//获得用户名
                    String oldPassword=oldPassword_Text.getText();
                    String sqlName="select Sid from student where Sid='"+ Upusername +"' && Spwd='"+oldPassword+"';";
                    int valid=DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "原密码错误，请重试");
                    }else {
                        String password1=newPassword_Text.getText();
                        String password2=newPassword_Text_2.getText();
                        if(password1.equals(password2)) {//两次输入的密码一样
                            String UpdatePassword="update student set Spwd='"+password1+"' where Sid='"+Upusername+"';";
                            try{
                                DBconnection_maDS.updateQuery(UpdatePassword);
                                JOptionPane.showMessageDialog(null, "修改成功");
                            }catch (Exception e1) {//修改密码时候出现错误，抛出异常

                                e1.printStackTrace();
                                JOptionPane.showMessageDialog(null, "有错误，请重试");
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "两次密码不一致，请重试");
                        }//
                    }
                }
                if(type==1) {//老师修改密码
                    String Upusername=Username_textField.getText();//获得用户名
                    String oldPassword=oldPassword_Text.getText();
                    String sqlName="select Tid from Teacher where Tid='"+ Upusername +"' && Tpwd='"+oldPassword+"';";
                    int valid=DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "原密码错误，请重试");
                    }else {
                        String password1=newPassword_Text.getText();
                        String password2=newPassword_Text_2.getText();
                        if(password1.equals(password2)) {//两次输入的密码一样
                            String UpdatePassword="update Teacher set Tpwd='"+password1+"' where Tid='"+Upusername+"';";
                            try{
                                DBconnection_maDS.updateQuery(UpdatePassword);
                                JOptionPane.showMessageDialog(null, "修改成功");
                            }catch (Exception e1) {//修改密码时候出现错误，抛出异常

                                e1.printStackTrace();
                                JOptionPane.showMessageDialog(null, "有错误，请重试");
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "两次密码不一致，请重试");
                        }//
                        UpdatePasswordDS.this.dispose();
                    }
                }
                if(type==2) {
                    String Upusername=Username_textField.getText();//获得用户名
                    String oldPassword=oldPassword_Text.getText();
                    String sqlName="select Aid from admin where Aid='"+ Upusername +"' && Apwd='"+oldPassword+"';";
                    int valid=DBconnection_maDS.FindUsername(sqlName);
                    if(valid==0) {
                        JOptionPane.showMessageDialog(null, "原密码错误，请重试");
                    }else {
                        String password1=newPassword_Text.getText();
                        String password2=newPassword_Text_2.getText();
                        if(password1.equals(password2)) {//两次输入的密码一样
                            String UpdatePassword="update admin set Apwd='"+password1+"' where Aid='"+Upusername+"';";
                            try{
                                DBconnection_maDS.updateQuery(UpdatePassword);
                                JOptionPane.showMessageDialog(null, "修改成功");
                            }catch (Exception e1) {//修改密码时候出现错误，抛出异常

                                e1.printStackTrace();
                                JOptionPane.showMessageDialog(null, "有错误，请重试");
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "两次密码不一致，请重试");
                        }//
                    }
                }
            }
        });
//		----------------------------修改密码
    }
}